package com.pwc.TEST;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.xml.namespace.QName;
import javax.xml.soap.*;

import com.pwc.soapUtil.SoapManager;

public class SOAPClientSAAJ {

	static String URLWorkDay = "https://wd2-impl-services1.workday.com/ccx/service/pwc_dpt1/Staffing/v25.0?wsdl";

//	static String WorkDayPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
//			+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wd=\"urn:com.workday/bsvc\">"
//			+ "<soapenv:Header><wsse:Security soapenv:mustUnderstand=\"1\" "
//			+ "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" "
//			+ "xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">"
//			+ "<wsse:UsernameToken wsu:Id=\"UsernameToken-31FD64F5DB72DD726114436045726833\">"
//			+ "<wsse:Username>hyang-impl@pwc_dpt1</wsse:Username>"
//			+ "<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">Underall1@</wsse:Password>"
//			+ "<wsse:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">OmOwyXPaJvaeiYnr1oUQoQ==</wsse:Nonce><wsu:Created>2015-09-30T09:16:12.683Z</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header>"
//			+ "<soapenv:Body>"
//			+ " <wd:Put_Applicant_Request wd:version=\"v25.0\">"
//			+ "<wd:Applicant_Data>"
//			+ "<wd:Personal_Data>"
//			+ "<wd:Name_Data>"
//			+ " <wd:Legal_Name_Data>"
//			+ " <wd:Name_Detail_Data>"
//			+ "<wd:Country_Reference>"
//			+ "<wd:ID wd:type=\"ISO_3166-1_Alpha-3_Code\">USA</wd:ID>"
//			+ "</wd:Country_Reference>"
//			+ "<wd:First_Name>Mess0</wd:First_Name>"
//			+ "<wd:Last_Name>Willam0</wd:Last_Name>"
//			+ "</wd:Name_Detail_Data>"
//			+ "</wd:Legal_Name_Data>"
//			+ "</wd:Name_Data>"
//			+ "<wd:Contact_Data>"
//			+ "<wd:Address_Data>"
//			+ "<wd:Country_Reference>"
//			+ "<wd:ID wd:type=\"ISO_3166-1_Alpha-2_Code\">US</wd:ID>"
//			+ "</wd:Country_Reference>"
//			+ "<wd:Address_Line_Data wd:Descriptor=\"Address Line 1\" wd:Type=\"ADDRESS_LINE_1\">116 Fairview St.</wd:Address_Line_Data>"
//			+ "<wd:Municipality>Hallsville</wd:Municipality>"
//			+ "<wd:Country_Region_Reference>"
//			+ "<wd:ID wd:type=\"Country_Region_ID\">USA-MO</wd:ID>"
//			+ "</wd:Country_Region_Reference>"
//			+ "<wd:Postal_Code>65255</wd:Postal_Code>"
//			+ "<wd:Usage_Data wd:Public=\"false\">"
//			+ "<wd:Type_Data wd:Primary=\"true\">"
//			+ "<wd:Type_Reference>"
//			+ "<wd:ID wd:type=\"Communication_Usage_Type_ID\">Home</wd:ID>"
//			+ " </wd:Type_Reference>"
//			+ "</wd:Type_Data>"
//			+ "</wd:Usage_Data>"
//			+ " </wd:Address_Data>"
//			+ "</wd:Contact_Data>"
//			+ "</wd:Personal_Data>"
//			+ "</wd:Applicant_Data>"
//			+ "</wd:Put_Applicant_Request>"
//			+ "</soapenv:Body>"
//			+ "</soapenv:Envelope>";
	
	
	
	
	
	static String WorkDayPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
			+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wd=\"urn:com.workday/bsvc\">"
			+ "<soapenv:Header/>"
			+ "<soapenv:Body>"
			+ " <wd:Put_Applicant_Request wd:version=\"v25.0\">"
			+ "<wd:Applicant_Data>"
			+ "<wd:Personal_Data>"
			+ "<wd:Name_Data>"
			+ " <wd:Legal_Name_Data>"
			+ " <wd:Name_Detail_Data>"
			+ "<wd:Country_Reference>"
			+ "<wd:ID wd:type=\"ISO_3166-1_Alpha-3_Code\">USA</wd:ID>"
			+ "</wd:Country_Reference>"
			+ "<wd:First_Name>Mess0</wd:First_Name>"
			+ "<wd:Last_Name>Willam0</wd:Last_Name>"
			+ "</wd:Name_Detail_Data>"
			+ "</wd:Legal_Name_Data>"
			+ "</wd:Name_Data>"
			+ "<wd:Contact_Data>"
			+ "<wd:Address_Data>"
			+ "<wd:Country_Reference>"
			+ "<wd:ID wd:type=\"ISO_3166-1_Alpha-2_Code\">US</wd:ID>"
			+ "</wd:Country_Reference>"
			+ "<wd:Address_Line_Data wd:Descriptor=\"Address Line 1\" wd:Type=\"ADDRESS_LINE_1\">116 Fairview St.</wd:Address_Line_Data>"
			+ "<wd:Municipality>Hallsville</wd:Municipality>"
			+ "<wd:Country_Region_Reference>"
			+ "<wd:ID wd:type=\"Country_Region_ID\">USA-MO</wd:ID>"
			+ "</wd:Country_Region_Reference>"
			+ "<wd:Postal_Code>65255</wd:Postal_Code>"
			+ "<wd:Usage_Data wd:Public=\"false\">"
			+ "<wd:Type_Data wd:Primary=\"true\">"
			+ "<wd:Type_Reference>"
			+ "<wd:ID wd:type=\"Communication_Usage_Type_ID\">Home</wd:ID>"
			+ " </wd:Type_Reference>"
			+ "</wd:Type_Data>"
			+ "</wd:Usage_Data>"
			+ " </wd:Address_Data>"
			+ "</wd:Contact_Data>"
			+ "</wd:Personal_Data>"
			+ "</wd:Applicant_Data>"
			+ "</wd:Put_Applicant_Request>"
			+ "</soapenv:Body>"
			+ "</soapenv:Envelope>";
	
	

	public static void main(String args[]) throws Exception {
		// Create SOAP Connection
		SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory
				.newInstance();
		SOAPConnection soapConnection = soapConnectionFactory
				.createConnection();

		// Send SOAP Message to SOAP Server
		// String url =
		// "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
		
		SOAPMessage message=getMessage(WorkDayPayload);
		
		SOAPMessage securitymessage=addSecurity(message,"hyang-impl@pwc_dpt1","Underall1@");
		
		SOAPMessage soapResponse = soapConnection.call(
				securitymessage, URLWorkDay);

		// print SOAP Response
		System.out.println("Response SOAP Message:");
		soapResponse.writeTo(System.out);

		soapConnection.close();
	}

	private static SOAPMessage getMessage(String send) throws IOException,
			SOAPException {
		InputStream is = new ByteArrayInputStream(send.getBytes());
		SOAPMessage request = MessageFactory.newInstance().createMessage(null,
				is);
		return request;
	}

	private static SOAPMessage addSecurity(SOAPMessage soapMessage, String userName,String password) throws Exception {
		

		SOAPHeader header = soapMessage.getSOAPHeader();

		SOAPElement security = header
				.addChildElement(
						"Security",
						"wsse",
						"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

		SOAPElement usernameToken = security.addChildElement("UsernameToken",
				"wsse");
		usernameToken
				.addAttribute(
						new QName("xmlns:wsu"),
						"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

		SOAPElement userNamePart = usernameToken
				.addChildElement("Username", "wsse");
		userNamePart.addTextNode(userName);

		SOAPElement passwordPart = usernameToken
				.addChildElement("Password", "wsse");
		passwordPart.setAttribute(
				"Type",
				"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
		passwordPart.addTextNode(password);

		SOAPElement nonce = usernameToken.addChildElement("Nonce", "wsse");
		nonce.setAttribute(
				"EncodingType",
				"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
		nonce.addTextNode("OmOwyXPaJvaeiYnr1oUQoQ==");

		SOAPElement created = usernameToken.addChildElement("Created", "wsse");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
		Calendar c1 = Calendar.getInstance();
		created.addTextNode(sdf.format(c1.getTime()));

		return soapMessage;
	}

	private static SOAPMessage createSOAPRequest() throws Exception {
		MessageFactory messageFactory = MessageFactory.newInstance();
		SOAPMessage soapMessage = messageFactory.createMessage();
		SOAPPart soapPart = soapMessage.getSOAPPart();

		String serverURI = "http://ws.cdyne.com/";

		// SOAP Envelope
		SOAPEnvelope envelope = soapPart.getEnvelope();
		envelope.addNamespaceDeclaration("example", serverURI);

		/*
		 * Constructed SOAP Request Message: <SOAP-ENV:Envelope
		 * xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
		 * xmlns:example="http://ws.cdyne.com/"> <SOAP-ENV:Header/>
		 * <SOAP-ENV:Body> <example:VerifyEmail>
		 * <example:email>mutantninja@gmail.com</example:email>
		 * <example:LicenseKey>123</example:LicenseKey> </example:VerifyEmail>
		 * </SOAP-ENV:Body> </SOAP-ENV:Envelope>
		 */

		// SOAP Body
		SOAPBody soapBody = envelope.getBody();
		SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail",
				"example");
		SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email",
				"example");
		soapBodyElem1.addTextNode("mutantninja@gmail.com");
		SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey",
				"example");
		soapBodyElem2.addTextNode("123");

		MimeHeaders headers = soapMessage.getMimeHeaders();
		headers.addHeader("SOAPAction", serverURI + "VerifyEmail");

		soapMessage.saveChanges();

		/* Print the request message */
		System.out.print("Request SOAP Message:");
		soapMessage.writeTo(System.out);
		System.out.println();

		return soapMessage;
	}

}